package at.fhooe.tof.mockup;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import at.fhooe.tof.app.MainProgram;
import at.fhooe.tof.common.Common;

public class TOFStreamMockup{
	private byte[] data;
	private boolean imageRead = false;
	private byte[] imgBytes = new byte[120*160*4];
	private float[] imgFloats = new float[120*160];
	
	public boolean getStatus(){
		return imageRead;
	}
	
	public byte[] getImage(){
		return imgBytes;
	}
	
	public float[] getImageFloat(){
		return imgFloats;
	}
	
	public void init(){
		try {
			File file = new File(".\\tofs\\dump-2013-04-16-140357.tof");
	    	Path path = file.toPath();
			data = Files.readAllBytes(path);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void streamMockup(int fromIndex){
		try{
			
			int byteIndex = 0;
			byte[] imageArray = new byte[160*120*4];
			for(int j=fromIndex; j<data.length; j++){
				if(byteIndex<160*120*4){
					imageRead = false;
					imageArray[byteIndex] = data[j];
					byteIndex++;
				}else{
					imgBytes = imageArray;
					imageArray = new byte[160*120*4];
					imageRead = true;
					//TOFMockupEvent event = new TOFMockupEvent(this);
					TOFMockupEventSource src = MainProgram.getSrcListener();
					src.fireEvent();
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void streamMockupFloat(int fromIndex){
		try{
			
			int byteIndex = 0;
			float[] imageFloat = new float[160*120];
			int inc = 0;
			
			for(int j=fromIndex; j<data.length; j+=4){
				if(byteIndex<160*120*4){
					imageRead = false;
					byte[] byteArray = new byte[4];
					byteArray[0]= data[j];
					byteArray[1]= data[j+1];
					byteArray[2]= data[j+2];
					byteArray[3]= data[j+3];
					imageFloat[inc] = Common.convert4BytesToFloat(byteArray);
					inc++;
					byteIndex+=4;
				}else{
					imgFloats = imageFloat;
					imageFloat = new float[160*120];
					imageRead = true;
					//TOFMockupEvent event = new TOFMockupEvent(this);
					TOFMockupEventSource src = MainProgram.getSrcListener();
					src.fireEvent();
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
